using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class FishPanel : MonoBehaviour,IInitializableUI
{
    [SerializeField] private Transform contentParent; 
    [SerializeField] private GameObject fishItemPrefab;
    [SerializeField] private GameObject TotalNumber;
    [SerializeField] private GameObject TotalValue;
    
    [SerializeField] private GameObject MaxValue;
    [SerializeField] private GameObject MaxWeight;

    [SerializeField] private GameObject ValueImage;
    [SerializeField] private GameObject WeightImage;
    
    private FishGameData currentData;
    
    // 初始化面板
    public void InitializePanel()
    {
        currentData = PlayerPrefs.HasKey("FishGameData") 
            ? JsonUtility.FromJson<FishGameData>(PlayerPrefs.GetString("FishGameData"))
            : new FishGameData();
        ClearPanel();
        
        TotalNumber.GetComponent<TextMeshProUGUI>().text = currentData.FishCount.ToString();
        
        MaxValue.GetComponent<TextMeshProUGUI>().text = currentData.MaxValue.ToString();
        MaxWeight.GetComponent<TextMeshProUGUI>().text = currentData.MaxWeight.ToString();
        TotalValue.GetComponent<TextMeshProUGUI>().text = currentData.TotalValue.ToString();

        
        //todo 动态更改 - 名字ID
        Sprite ValueImagee = Resources.Load<Sprite>("hero");
        Sprite WeightImagee = Resources.Load<Sprite>("hero");
        ValueImage.GetComponent<Image>().sprite = ValueImagee;
        WeightImage.GetComponent<Image>().sprite = WeightImagee;
        
        PopulateFishItems();
    }

    // 生成所有鱼条目
    private void PopulateFishItems()
    {
        if (currentData == null || currentData.collectedItems == null) return;
        
        print(currentData.collectedItems[0].TotalValue);
        
        foreach (FishSaveData fish in currentData.collectedItems)
        {
            CreateFishItem(fish);
        }
    }
    private void CreateFishItem(FishSaveData fishSaveData)
    {
        GameObject newItem = Instantiate(fishItemPrefab, contentParent);
        FishItemUI itemUI = newItem.GetComponent<FishItemUI>();
        
        if (itemUI != null)
        {
            itemUI.Initialize(
                fishSaveData.Name,
                fishSaveData.Count.ToString(),
                fishSaveData.TotalValue
            );
            //itemUI.SetIcon(Resources.Load<Sprite>($"Icons/{fishSaveData.Name}"));
        }
    }
    // 清空面板
    private void ClearPanel()
    {
        foreach (Transform child in contentParent)
        {
            Destroy(child.gameObject);
        }
    }

    // 更新数据（在数据变化时调用）
    public void RefreshPanel(FishGameData newData)
    {
        currentData = newData;
        ClearPanel();
        PopulateFishItems();
    }
    
}
